<template>
  <div :class="className" :style="{height:height,width:width}" />
</template>

<script>
import echarts from 'echarts'
require('echarts/theme/macarons') // echarts theme
import resize from './mixins/resize'
import {optionsAction} from '@/api/statistics'
export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'chart'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '300px'
    },
    autoResize: {
      type: Boolean,
      default: true
    }
  },
  data() {
    return {
      chart: null,
      datas:{},
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initData();
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = echarts.init(this.$el, 'macarons')
      this.setOptions()
    },
    initData(){
      optionsAction("inventory").then(response=>{
        this.datas =  response.data;
      }).finally(()=>{
         this.initChart()
      })
    },
    setOptions({ expectedData, actualData } = {}) {
      this.chart.setOption({
        tooltip: {
          trigger: 'item',
          formatter: '{a} <br/>{b} : {c}'
        },
        legend: {},
        color:['#5470c6','#73c0de','#91cc75', '#fac858', '#ee6666',  '#9a60b4','#3ba272', '#fc8452', '#ea7ccc'],
        grid: {
          left: '1%',
          right: '3%',
          bottom: '2%',
          containLabel: true
        },
        xAxis: {
            type: 'category',
            data:this.datas.xData
          },
          yAxis: {
            type: 'value'
          },
          series: [
            {
              name: '入库数',
              data:this.datas.yData.innums,
              type: 'bar',
              showBackground: true,
              backgroundStyle: {
                color: 'rgba(180, 180, 180, 0.2)'
              }
            },
            {
              name: '出库数',
              data:this.datas.yData.outnums,
              type: 'bar',
              showBackground: true,
              backgroundStyle: {
                color: 'rgba(180, 180, 180, 0.2)'
              }
            },
            {
              name: '处置数',
              data:this.datas.yData.deals,
              type: 'bar',
              showBackground: true,
              backgroundStyle: {
                color: 'rgba(180, 180, 180, 0.2)'
              }
            },
            {
              name: '库存数',
              data:this.datas.yData.nums,
              type: 'bar',
              showBackground: true,
              backgroundStyle: {
                color: 'rgba(180, 180, 180, 0.2)'
              }
            },

          ]
      })
    }
  }
}
</script>
